/**
 * 获取URL参数
 * @param {} paras 参数名
 * @return {String}
 */
function request(paras)
{ 
    var url = location.href; 
    var paraString = url.substring(url.indexOf("?")+1,url.length).split("&"); 
    var paraObj = {} 
    for (i=0; j=paraString[i]; i++){ 
    	paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length); 
    } 
    var returnValue = paraObj[paras.toLowerCase()]; 
    if(typeof(returnValue)=="undefined"){ 
    	return ""; 
    }else{ 
    	return returnValue; 
    } 
}



$(function(){
	$('input[type="text"]').addClass('input_text');

	/**
	 * 列名
	 */
	var colNames = ["角色ID主键","角色名称","角色描述"];
	
	/**
	 * 列模型
	 */
	var colModel = [
		{name:'roleid',index:'roleid',hidden:true},
		{name:'rolename',index:'rolename',width:100},
		{name:'roledesc',index:'roledesc',width:140}
	];
	
	/**
	 * 角色列表
	 */
	var fromGrid = $("#fromTable").jqGrid({
		url:common_basePath+'/role/searchUserRole.action',
		datatype: "json",
		width: 300,
		height:350,
		colNames: colNames,
		colModel:colModel,
		shrinkToFit:false,
		sortname:'roleid',
		sortorder:'desc',
		viewrecords:true,
		multiselect: true, // 是否显示复选框
		gridview: true,  //提升速度
        ondblClickRow : function(rowid){
            var row = $("#fromTable").jqGrid('getRowData',rowid);
            $("#toTable").jqGrid('addRowData',rowid,row);
            $("#fromTable").jqGrid('delRowData',rowid);
        },
		rowNum:100,
		rowList:[10,20,30],
		//toolbar: [true,"top"],
		jsonReader: {
			root:"roles",		// 数据行（默认为：rows）
			page: "page",  	// 当前页
			total: "total",  // 总页数
			records: "records",  // 总记录数
			repeatitems : false		// 设置成false，在后台设置值的时候，可以乱序。且并非每个值都得设
		},
		prmNames:{rows:"rows",page:"page",sort:"ROLEID",order:"sord",search:"search"},
		postData: {
			toIds:request('roleIds')
		},
		//pager:"#fromPager",
		caption: "角色"
	});
	
	/**
	 * 已选列表
	 */
	var toGrid = $("#toTable").jqGrid({
		url:common_basePath+'/role/searchUserRole.action',
		datatype: "json",
		width: 300,
		height:350,
		colNames: colNames,
		colModel:colModel,
		shrinkToFit:false,
		sortname:'roleid',
		sortorder:'desc',
		viewrecords:true,
		multiselect: true, // 是否显示复选框
		gridview: true,  //提升速度
        ondblClickRow : function(rowid){
            var row = $("#toTable").jqGrid('getRowData',rowid);
            $("#fromTable").jqGrid('addRowData',rowid,row);
            $("#toTable").jqGrid('delRowData',rowid);
        },		
		rowNum:100,
		rowList:[10,20,30],
		jsonReader: {
			root:"roles",		// 数据行（默认为：rows）
			page: "page",  	// 当前页
			total: "total",  // 总页数
			records: "records",  // 总记录数
			repeatitems : false		// 设置成false，在后台设置值的时候，可以乱序。且并非每个值都得设
		},
		postData: {
			fromIds:request('roleIds')?request('roleIds'):"0"
		},
		//pager:"#toPager",
		caption: "已选角色"
	});	
	
    /*****************以下部分为选择与取消*****************/
    $("#to").click(function(){
        var s = $("#fromTable").jqGrid('getGridParam','selarrrow')+"";
        var to = $("#toTable").jqGrid('getDataIDs')+"";
        if(s){
            var ids = s.split(",");
            var toIds = to?to.split(","):[];
            for(var i=0;i<ids.length;i++){
                var id = ids[i];
                var row = $("#fromTable").jqGrid('getRowData',id);
                var flag = true;
                for(var j=0;j<toIds.length;j++){
                	var toId = toIds[j];
                    var toRow = $("#toTable").jqGrid('getRowData',toId);
                    if(row['roleid']==toRow['roleid']){
                    	flag = false;
                    	break;
                    }
                }
                if(flag){
                    $("#toTable").jqGrid('addRowData',id,row);
                    $("#fromTable").jqGrid('delRowData',id);
                }
            }
        }
        
        var to = $("#toTable").jqGrid('getDataIDs')+"";
        var toIdArr = [];
        var toIds = to?to.split(","):[];
        for(var j=0;j<toIds.length;j++){
            var toId = toIds[j];
            var toRow = $("#toTable").jqGrid('getRowData',toId);
            toIdArr.push(toRow['roleid']);
        }
        $("#fromTable").jqGrid('setGridParam',
        {
            postData:{toIds:toIdArr.join()}
        });
    });
    
    $("#toAll").click(function(){
        var s = $("#fromTable").jqGrid('getDataIDs')+"";
        var to = $("#toTable").jqGrid('getDataIDs')+"";
        if(s){
            var ids = s.split(",");
            var toIds = to?to.split(","):[];
            for(var i=0;i<ids.length;i++){
                var id = ids[i];
                var row = $("#fromTable").jqGrid('getRowData',id);
                var flag = true;
                for(var j=0;j<toIds.length;j++){
                    var toId = toIds[j];
                    var toRow = $("#toTable").jqGrid('getRowData',toId);
                    if(row['roleid']==toRow['roleid']){
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    $("#toTable").jqGrid('addRowData',id,row);
                    $("#fromTable").jqGrid('delRowData',id);
                }
            }
        }
        var to = $("#toTable").jqGrid('getDataIDs')+"";
        var toIdArr = [];
        var toIds = to?to.split(","):[];
        for(var j=0;j<toIds.length;j++){
            var toId = toIds[j];
            var toRow = $("#toTable").jqGrid('getRowData',toId);
            toIdArr.push(toRow['roleid']);
        }
        $("#fromTable").jqGrid('setGridParam',
        {
            postData:{toIds:toIdArr.join()}
        }).trigger("reloadGrid", [{page:1}]);
    });
    
    $("#from").click(function(){
        var s = $("#toTable").jqGrid('getGridParam','selarrrow')+"";
        var to = $("#fromTable").jqGrid('getDataIDs')+"";
        if(s){
            var ids = s.split(",");
            var toIds = to?to.split(","):[];
            for(var i=0;i<ids.length;i++){
                var id = ids[i];
                var row = $("#toTable").jqGrid('getRowData',id);
                var flag = true;
                for(var j=0;j<toIds.length;j++){
                    var toId = toIds[j];
                    var toRow = $("#fromTable").jqGrid('getRowData',toId);
                    if(row['roleid']==toRow['roleid']){
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    $("#fromTable").jqGrid('addRowData',id,row);
                    $("#toTable").jqGrid('delRowData',id);
                }
            }
        }
        var to = $("#toTable").jqGrid('getDataIDs')+"";
        var toIdArr = [];
        var toIds = to?to.split(","):[];
        for(var j=0;j<toIds.length;j++){
            var toId = toIds[j];
            var toRow = $("#toTable").jqGrid('getRowData',toId);
            toIdArr.push(toRow['roleid']);
        }
        $("#fromTable").jqGrid('setGridParam',
        {
            postData:{toIds:toIdArr.join()}
        });
    });
    
    $("#fromAll").click(function(){
        var s = $("#toTable").jqGrid('getDataIDs')+"";
        var to = $("#fromTable").jqGrid('getDataIDs')+"";
        if(s){
            var ids = s.split(",");
            var toIds = to?to.split(","):[];
            for(var i=0;i<ids.length;i++){
                var id = ids[i];
                var row = $("#toTable").jqGrid('getRowData',id);
                var flag = true;
                for(var j=0;j<toIds.length;j++){
                    var toId = toIds[j];
                    var toRow = $("#fromTable").jqGrid('getRowData',toId);
                    if(row['roleid']==toRow['roleid']){
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    $("#fromTable").jqGrid('addRowData',id,row);
                    $("#toTable").jqGrid('delRowData',id);
                }
            }
        }
        
        var to = $("#toTable").jqGrid('getDataIDs')+"";
        var toIdArr = [];
        var toIds = to?to.split(","):[];
        for(var j=0;j<toIds.length;j++){
            var toId = toIds[j];
            var toRow = $("#toTable").jqGrid('getRowData',toId);
            toIdArr.push(toRow['roleid']);
        }
        $("#fromTable").jqGrid('setGridParam',
        {
            postData:{toIds:toIdArr.join()}
        });
    });
    
    /**
     * 确定
     */
    $("#entry").click(function(){
        var result = [];
        var s = $("#toTable").jqGrid('getDataIDs')+"";
        if(s){
            var ids = s.split(",");
            for(var i=0;i<ids.length;i++){
                var id = ids[i];
                var row = $("#toTable").jqGrid('getRowData',id);
                result.push(row);
            }
        }
        window.returnValue = result;
        window.close();
    });
    
    /**
     * 取消
     */
    $("#center").click(function(){
        window.close();
    });
});

/**
 * 查询
 */
var gridSearch = function(){
    var rolename = $("#rolename").val();
    var params = {
        "rolename" : encodeURIComponent(rolename)
    };
        
    var postData = $("#fromTable").jqGrid("getGridParam", "postData");
    $.extend(postData, params);  //(9)将postData中的查询参数覆盖为空值
    $("#fromTable").jqGrid('setGridParam',
    {
        url:common_basePath+'/role/searchUserRole.action'
    }).trigger("reloadGrid", [{page:1}]); 
}

//清空
var reset = function(){
    jQuery("#rolename").val('');
}
